<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Option Login User Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  .bold { font-weight: bold; }
  input { margin: 5px; width: 20px; } 
  .hide { display: none; }
  input[id="password"] { width: 120px; }
  input[id="username"] { width: 120px; }
  /* 160 + 80 + 50 + 10 */
  #wrap,
  #sync-task-cover {
    height: 300px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 54px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // 2000 seconds

var createElements = function(div){
  var n = core.randi(2, 7);
  var clickNames = [];
  for(var i=0;i<n;i++) {
    var chname = ui_utils.generateString(2,8);
    var label = div.append('label')
    label.append('input').attr('type', 'radio').attr('id', 'ch'+i).attr('name', 'radio');
    label[0][0].innerHTML += chname;
    div.append('br');
    clickNames.push(chname);
  }
  var ix = core.randi(0, n);
  var qstr = clickNames[ix];
  return {query: qstr, index: ix};
}

var genProblem = function() {

  var option_state = -1.0;
  // click-option
  var div = d3.select('#boxes');
  div.html('');

  var correctElement = createElements(div);
  div.append('br');
  div.append('button').attr('id', 'subbtn1').attr('class', 'secondary-action').html('Submit');

  d3.select('#subbtn1').on('click', function(){
    option_state = d3.select('#ch'+correctElement.index)[0][0].checked ? 1.0 : -1.0;
    $('#form').removeClass('hide');
    $('#boxes').addClass('hide');
  });

  // login-user
  d3.select('#username')[0][0].value ='';
  d3.select('#password')[0][0].value ='';

  var user = core.sample(ui_utils.FIFTY_NAMES).toLowerCase();
  var password = ui_utils.generateString(2,6)

  // d3.select('#query').html('Select ' + correctElement.query + ' and click Submit, and then enter the <span class="bold">username</span> "' + user + '" and the <span class="bold">password</span> "' + password + '" into the text fields and press login.');
  d3.select('#query').html('Enter the <span class="bold">username</span> "' + user + '" and the <span class="bold">password</span> "' + password + '" into the text fields and press login, after selecting ' + correctElement.query + ' and clicking Submit.');

  d3.select('#subbtn2').on('click', function(){
    var u = d3.select('#username')[0][0].value;
    var p = d3.select('#password')[0][0].value;
    var login_r = (u === user && p === password) ? 1.0 : -1.0;
    $('#form').addClass('hide');
    $('#boxes').removeClass('hide');
    if (option_state == 1.0) {
      core.endEpisode(login_r, login_r > 0);
    } else {
      core.endEpisode(-1.0);
    }
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <div id="boxes">
      <button id="subbtn1" class="secondary-action">Submit</button>
    </div>
    <div id="form" class="hide">
      <p><label class="bold">Username</label><input type="text" id="username"></p>
      <p><label class="bold">Password</label><input type="password" id="password"></p>
      <button id="subbtn2" class="secondary-action">Login</button>
    </div>
  </div>
</div>
</body>
</html>
